package com.example.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.dto.UserDto;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.IUserService;
import com.example.demo.vo.UserVo;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private  final UserMapper userMapper;

    @Override
    public UserVo getUserVo(UserDto userDto) {
        List<User> user = this.lambdaQuery().eq(User::getName, "强哥").list();
        System.out.println(user);
        return userMapper.getUserOne(userDto);
    }

    @Override
    public User getUserByLmd(UserDto userDto) {
//        this.lambdaQuery().eq(User::getName, "<UNK>");
        return this.lambdaQuery().eq(User::getName, userDto.getName()).one();
    }

//    @Override
//    public User getUserByStr() {
////        this.lambdaQuery().eq(User::getName, "<UNK>");
//        return this.lambdaQuery().eq(User::getName, "开心").one();
//    }

    @Override
    public List<User> getUsers(UserDto userDto) {
        // 查列表
        List<User> users = list(new QueryWrapper<User>().lambda().eq( User::getName, userDto.getName() ));
        // System.out.println(users);

        // 分页列表
        IPage<User> page = new Page<>(1, 10);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "强哥");
        System.out.println(this.page(page, queryWrapper).getPages());


        return this.lambdaQuery().eq(User::getName, userDto.getName()).list().stream().map(a-> {
            return a;
        }).collect(Collectors.toList());
    }


    @Override
    public IPage<User> getUserByPager(UserDto userDto) {
        // 分页列表
        IPage<User> page = new Page<>(1, 10);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", "强哥");
        return this.page(page, queryWrapper);
    }

    @Override
    public int updateUser(UserDto userDto) {
        User user = new User();
        UpdateWrapper<User> updateUser = new UpdateWrapper<>();
        updateUser.eq("id", userDto.getId());
        updateUser.set("name", userDto.getName());
//        return this.update(user, updateUser);

        // 删除map
//        Map<String, Object> colMap = new HashMap<>();
//        colMap.put("id", 1);
//        this.removeByMap(colMap);

        // 删除list
//        List<Integer> ids = Arrays.asList(1, 2, 3);
//        removeByIds(ids);
//        removeById(1);

        //多个相等值
//        new QueryWrapper<>().allEq(Map.of("id", 1, "name", "jack"));

        return userMapper.update(user, updateUser);
    }
}
